<template>
  <div class="permission">
    <!-- <page-tools>
      <template #left>
        <span>有0条记录</span>
      </template>
      <template #right>
        <div>
          <el-button>测试</el-button>
          <el-button>导入</el-button>
        </div>
      </template>
    </page-tools> -->
    <el-card>
      <div style="text-align: right">
        <!--addEvent(1, '0')代表添加页面,'0',父组件的pid===子组件的id  -->
        <el-button type="primary" @click="addEvent(1, '0')">添加菜单</el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <div>
              <!-- 1是页面添加,通过作用域获取当前的id然后对页面进行添加值 -->
              <span v-if="row.type === 1">
                <el-button type="text" @click="addEvent(1,row.id)">添加页面</el-button>
                <el-button type="text" @click="addEvent(2,row.id)">添加按钮</el-button>
              </span>
              <el-button type="text" @click="edit(row)">编辑权限点</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <add ref="add" @getData="getData()" />
    </el-card>
  </div>
</template>

<script>
// 导入
import { sysPermission, sysPermissionDelete } from '@/api/permission.js'
// 导入组件
import add from './components/add.vue'
export default {
  components: {
    add
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysPermission()
      console.log(res, '权限')
      this.list = this.changeData(res.data, '0')
    },
    // 组织树形图子级
    changeData(arr, str = '0') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 通过$refs传值给form里面的值,点击新增
    addEvent(type, pid) {
      this.$refs.add.isShow = true
      this.$refs.add.form.type = type
      this.$refs.add.form.pid = pid
      this.$refs.add.mode = 'add'
    },
    // 删除
    del(row) {
      this.$confirm('你确定删除吗?', '提示')
        .then(async() => {
          await sysPermissionDelete(row)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
    },
    // 编辑
    edit(row) {
      console.log(row, '编辑')
      // 点击编辑,打开弹框
      this.$refs.add.isShow = true
      // 进行深拷贝
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      // 修改子组件的值mode为edit
      this.$refs.add.mode = 'edit'
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  padding: 15px;
}
</style>
